#include "stdafx.h"
#include "GSE_CMysqlDataSet.h"
namespace GSE
{
	CMysqlFieldInfo::CMysqlFieldInfo(MYSQL_RES * res)
	{
		uint uCount;
		uint i;
		MYSQL_FIELD *field;

		uCount = mysql_num_fields(res);
		for(i = 0; i < uCount; i++)
		{
			field = mysql_fetch_field_direct(res, i);
			mFieldMap[field->name] = i;
		}
	}

	CMysqlDataSet::CMysqlDataSet(MYSQL_RES* res):m_pFields(new CMysqlFieldInfo(res))
	{
		while (MYSQL_ROW r = mysql_fetch_row(res)) 
		{
			if (const unsigned long* lengths = mysql_fetch_lengths(res))
			{
				uint field_count = FieldCount();
				CRow::pointer temp_row = CRow::pointer(new CRow(m_pFields));
				for (unsigned long i = 0; i < field_count; i++)
				{
					temp_row->Push(r[i],lengths[i]);
				}
				m_Rows.push_back(temp_row);
			}
		}
	}


	uint CMysqlDataSet::FieldCount() const
	{
		return m_pFields->FieldCount();
	}

	uint CMysqlDataSet::FieldIndex(const char * strFieldName) const
	{
		return (*m_pFields)[strFieldName];
	}

	const IRow & CMysqlDataSet::operator [] (int index) const
	{
		return *m_Rows[index];
	}
}